package com.itchen.web;

import com.itchen.pojo.User;
import com.itchen.service.UserService;
import com.itchen.util.CheckCodeUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @author げっしょく
 * @date 2021/9/4
 */
@WebServlet("/addUser")
public class AddUser extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        String checkCode = request.getParameter("checkCode");
        HttpSession session1 = request.getSession();
        String checkcode = (String) session1.getAttribute("checkcode");

        UserService userService = new UserService();

        if (checkcode.equalsIgnoreCase(checkCode)) {
            if (userService.selectUserName(user)) {
                request.setAttribute("login_msg", "注册成功，请登录");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            } else {
                request.setAttribute("login_msg", "用户名已被注册");
                request.getRequestDispatcher("register.jsp").forward(request, response);
            }


        } else {
            request.setAttribute("login", "验证码输入有误");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
